#pragma once
#ifndef LED_H
#define LED_H

#include "driver/rmt.h"
#include "esp32-hal.h"
// #ifdef __AVR__
//  #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
// #endif
// #include "FastLED.h"

// How many leds in your strip?
#define NUM_LEDS 10

#define DATA_PIN 3
#define CLOCK_PIN 13
#define DELAY_TIME 250

#ifndef MAX
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif

#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
#define _LIMIT(a, b) (a > b ? b : a)
// tm1914第一二帧为24位控制器编码
#define NR_OF_LEDS(num) num
#define NR_OF_ALL_BITS 24 * NR_OF_LEDS

#if CONFIG_DISABLE_HAL_LOCKS
#define RMT_MUTEX_LOCK(channel)
#define RMT_MUTEX_UNLOCK(channel)
#else
#define RMT_MUTEX_LOCK(channel) \
    do                          \
    {                           \
    } while (xSemaphoreTake(g_rmt_objlocks[channel], portMAX_DELAY) != pdPASS)
#define RMT_MUTEX_UNLOCK(channel) xSemaphoreGive(g_rmt_objlocks[channel])
#endif /* CONFIG_DISABLE_HAL_LOCKS */
#define MAX_CHANNELS (SOC_RMT_GROUPS * SOC_RMT_CHANNELS_PER_GROUP)
/**
 * Internal variables for channel descriptors
 */
static xSemaphoreHandle g_rmt_objlocks[MAX_CHANNELS] = {
    NULL, NULL, NULL, NULL,
#if MAX_CHANNELS > 4
    NULL, NULL, NULL, NULL
#endif
};

typedef enum
{
    WS2811 = 0,
    TM1914 = 1
} led_chipLet;

typedef enum
{
    LEDNORMAL = 0,
    RED = 1,
    GREEN = 2,
    BLUE = 3,
    POINT_TEST = 4,
    COLOR_CHANGE = 5
} led_mode;

#define ADAFRUIT_RMT_CHANNEL_MAX RMT_CHANNEL_MAX
class HYled
{
public:
    HYled(led_chipLet chiplet, int ledpixels);
    HYled(led_chipLet chiplet);
    HYled();
    ~HYled();
    void ledInit(led_chipLet ledChipLet);
    void normalLED(uint8_t &light, uint8_t red, uint8_t green, uint8_t blue);
    // void tm1914_writebyte_byt(unsigned char dat);
    void led_all_writebyte_byt(uint8_t r, uint8_t g, uint8_t b);
    void tm1914_init();
    void ledShow();
    // void testhigh();
    // void testlow();

    void setLedPixels(int ledpixels);
    void setLedMode(led_mode ledmode, uint8_t &light, uint8_t red, uint8_t green, uint8_t blue);
    void ledClear();
    void ledClear(uint32_t index);

private:
    int pixels = 100;
    int oldPixels = 0;
    rmt_data_t *led_data = NULL;

    led_chipLet chiplet;

    int color[3] = {0, 0, 0}; // RGB value
    // int led_index = 0;
    // rmt_obj_t *rmt_send = NULL;

    rmt_config_t config;
    rmt_tx_config_t rmtTXConfig;

    /*tm1914 C1、C2模式设置命令 0xFFFFFA_000000 */
    int c1[3] = {0xFF, 0xFF, 0xFF};
    int c2[3] = {0x00, 0x00, 0x00};
    bool rmt_reserved_channels[ADAFRUIT_RMT_CHANNEL_MAX];

    rmt_channel_t channel;
    void send_c1c2();
    void ws2811_init();

    void led_test_red();

    void led_test_green();

    void led_test_blue();

    void led_point_test();
    void led_color_change();
    void led_writebyte_byt(uint8_t byte, uint32_t start, uint32_t level0, uint32_t duration0, uint32_t level1, uint32_t duration1);
};

static esp_err_t light_driver_hsv2rgb(uint16_t hue, uint8_t saturation, uint8_t value,
                                      uint8_t *red, uint8_t *green, uint8_t *blue);
static void light_driver_rgb2hsv(uint16_t red, uint16_t green, uint16_t blue,
                                 uint16_t *h, uint8_t *s, uint8_t *v);

#endif // !LED_H
